package com.fjut.kms.dao;

import com.fjut.kms.domain.UserActSoft;
import com.fjut.kms.tools.DBTools;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Empathy on 2017/8/20.
 */
public class UserActSoftJDBCImpl implements UserActSoftDao {
    private static final String SQL_ADD="insert into userActSoft values(null,?,?,?,?,?,?,?,?,?)";
    private static final String SQL_UPDATE="update userActSoft set reqDate=?,status=?,ip=?,MAC=?,machineId=?,os=? where softName=?,userName=?";
    private static final String SQL_GET_BYUSERNAME="select machineId from userActSoft where softName=? and userName=? and status=?";

    public void add(UserActSoft uas){
        Connection conn= DBTools.getConn();
        PreparedStatement pstmt=null;
        try{
            pstmt =conn.prepareStatement(SQL_ADD);
            pstmt.setString(1,uas.getSoftName());
            pstmt.setString(2,uas.getUserName());
            pstmt.setString(3,uas.getReqDate());
            pstmt.setString(4,uas.getExpireDate());
            pstmt.setString(5,uas.getStatus());
            pstmt.setString(6,uas.getIp());
            pstmt.setString(7,uas.getMac());
            pstmt.setString(8,uas.getMachineId());
            pstmt.setString(9,uas.getOs());
            pstmt.executeUpdate();
        } catch (SQLException e){
            e.printStackTrace();
        }finally {
            DBTools.releaseRes(conn,pstmt,null);
        }
    }

    public void update(UserActSoft uas){
        Connection conn= DBTools.getConn();
        PreparedStatement pstmt=null;
        try{
            pstmt=conn.prepareStatement(SQL_UPDATE);
            pstmt.setString(1,uas.getReqDate());
            pstmt.setString(2,uas.getStatus());
            pstmt.setString(3,uas.getIp());
            pstmt.setString(4,uas.getMac());
            pstmt.setString(5,uas.getMachineId());
            pstmt.setString(6,uas.getOs());
            pstmt.setString(7,uas.getSoftName());
            pstmt.setString(8,uas.getUserName());
            pstmt.executeUpdate();
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            DBTools.releaseRes(conn,pstmt,null);
        }
    }

    public List<UserActSoft> getByUserName(String softName, String userName, String status){
        Connection conn= DBTools.getConn();
        PreparedStatement pstmt=null;
        ResultSet rset =null;
        List<UserActSoft> uasList=new ArrayList<UserActSoft>();
        try{
            pstmt=conn.prepareStatement(SQL_GET_BYUSERNAME);
            pstmt.setString(1,softName);
            pstmt.setString(2,userName);
            pstmt.setString(3,status);
            pstmt.executeQuery();
            while (rset.next()){
                UserActSoft uas=null;
                uas.setMachineId(rset.getString("machinId"));
                uasList.add(uas);
            }

        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            DBTools.releaseRes(conn,pstmt,null);
        }
        return uasList!=null?uasList:null;
    }

}
